﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using OgilvyOne.IDAL;
using OgilvyOne.Model;

namespace OgilvyOne.SqlServerDAL
{
    public class CofcoDayTip : ICofcoDayTip
    {
        private const string UPDATE_SQL = "update COFCO_DAY_TIP set DAY_IMG1=@DAY_IMG1,DAY_IMG2=@DAY_IMG2,DAY_IMG3=@DAY_IMG3,DAY_IMG4=@DAY_IMG4 where DAY_ID=@DAY_ID";
        //修改基本资料
        public bool Update(CofcoDayTipModel model)
        {
            SqlParameter[] parms = new SqlParameter[] { 
                new SqlParameter("@DAY_IMG1", SqlDbType.VarChar,50),
                new SqlParameter("@DAY_IMG2",SqlDbType.VarChar,50),
                new SqlParameter("@DAY_IMG3", SqlDbType.VarChar,50),
                new SqlParameter("@DAY_IMG4",SqlDbType.VarChar,50),
                new SqlParameter("@DAY_ID",SqlDbType.Int,4)
               
                
            };
            parms[0].Value = model.DayImg1;
            parms[1].Value = model.DayImg2;
            parms[2].Value = model.DayImg3;

            parms[3].Value = model.DayImg4;
            parms[4].Value = model.DayId;
            


            int i = SQLHelper.ExecuteSql(UPDATE_SQL, parms);
            if (i > 0) return true;
            return false;
        }
        private const string GET_IMG_SQL = "select DAY_ID,DAY_IMG1,DAY_IMG2,DAY_IMG3,DAY_IMG4 from COFCO_DAY_TIP where DAY_ID=@DAY_ID";
        public CofcoDayTipModel GetModel(int id)
        {
            CofcoDayTipModel mim = null;
            SqlParameter[] parms = new SqlParameter[] { 
                new SqlParameter("@DAY_ID", SqlDbType.Int, 4)
                
            };
            parms[0].Value = id;

            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, GET_IMG_SQL, parms))
            {
                if (rdr.Read())
                {
                    mim = new CofcoDayTipModel();
                    mim.DayId = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))  mim.DayImg1 = rdr.GetString(1);
                    if (!rdr.IsDBNull(2)) mim.DayImg2 = rdr.GetString(2);
                    if (!rdr.IsDBNull(3)) mim.DayImg3 = rdr.GetString(3);

                    if (!rdr.IsDBNull(4)) mim.DayImg4 = rdr.GetString(4);
                    
                }
            }
            return mim;
        }

        public IList GetList()
        {
            string qsql = "select DAY_ID,DAY_IMG1,DAY_IMG2,DAY_IMG3,DAY_IMG4 from COFCO_DAY_TIP";

           
            IList datalist = new ArrayList();
            SqlParameter[] parms = new SqlParameter[] { };

            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, qsql, parms))
            {
                while (rdr.Read())
                {
                    CofcoDayTipModel mim = new CofcoDayTipModel();
                    mim.DayId = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1)) mim.DayImg1 = rdr.GetString(1);
                    if (!rdr.IsDBNull(2)) mim.DayImg2 = rdr.GetString(2);
                    if (!rdr.IsDBNull(3)) mim.DayImg3 = rdr.GetString(3);

                    if (!rdr.IsDBNull(4)) mim.DayImg4 = rdr.GetString(4);
                    datalist.Add(mim);

                }
            }
            return datalist;
        }
    }
}
